package com.amap.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.amap.R;
import com.amap.controller.ApplicationController;

public class GetLocationActivity extends Activity {

	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.location);
		
		    final TextView acc = (TextView)findViewById(R.id.txtAccuracy);
	        final TextView alt = (TextView)findViewById(R.id.txtAltitude);
	        final TextView lat = (TextView)findViewById(R.id.txtLatitude);
	        final TextView longi = (TextView)findViewById(R.id.txtLongitude);
	        final TextView pro = (TextView)findViewById(R.id.txtProvider);
	        final TextView spd = (TextView)findViewById(R.id.txtSpeed);
	        final TextView proList = (TextView)findViewById(R.id.txtProvList);
	        
	        final GetLocationActivity glActivity = this;
	        
			Button retrieve = (Button) findViewById(R.id.btnRetrieve);
			retrieve.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					LocationManager LM = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
					String bestProvider = LM.getBestProvider(new Criteria(), true);
					// System.out.println("*************"+LM.getBestProvider(new
					// Criteria(),true));
					Location l = LM.getLastKnownLocation(bestProvider);
					if (l != null) {
						acc.setText(String.valueOf(l.getAccuracy()));
						alt.setText(String.valueOf(l.getAltitude()));
						lat.setText(String.valueOf(l.getLatitude()));
						longi.setText(String.valueOf(l.getLongitude()));
						pro.setText(String.valueOf(l.getProvider()));
						spd.setText(String.valueOf(l.getSpeed()));
						proList.setText(String.valueOf(LM.getAllProviders()));
						ApplicationController.getInstance().setUserLoc(new com.amap.model.Location(l.getLatitude(), l.getLongitude()));
						Intent i = new Intent();
						i.setClass(glActivity, MenuActivity.class);
						startActivity(i);
					} else {
						acc.setText(R.string.noInfo);
						alt.setText(R.string.noInfo);
						lat.setText(R.string.noInfo);
						longi.setText(R.string.noInfo);
						pro.setText(R.string.noInfo);
						spd.setText(R.string.noInfo);
						proList.setText(R.string.noInfo);
					}
				}
			});
		
	}
	
	
}
